package com.jingbian.cppccbackend.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jingbian.cppccbackend.entity.Admins;
import com.jingbian.cppccbackend.entity.Users;
import com.jingbian.cppccbackend.mapper.admin.AdminsMapper;
import com.jingbian.cppccbackend.service.admin.AdminsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingbian.cppccbackend.entity.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * <p>
 * 管理员信息表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-02-09
 */
@Service
public class AdminsServiceImpl extends ServiceImpl<AdminsMapper, Admins> implements AdminsService {
    @Autowired
    private AdminsMapper adminsMapper;
    
    @Override
    public Admins findByAccount(String account) {
        return adminsMapper.findByAccount(account);
    }

    @Override
    public List<Users> getUserlist() {
        return List.of();
    }

    @Override
    public Admins findById(Integer aid) {
        return adminsMapper.findById(aid);
    }

    @Override
    public PageVO<Users> getAllUsers(Integer pageNum, Integer pageSize, String searchText, String startDate, String endDate, String sortType, String status, String identity, String region, String affiliation, String others) {
        return null;
    }

    @Override
    public PageVO<Users> getApproveUsers(Integer pageNum, Integer pageSize, String searchText, String startDate, String endDate, String sortType, String status, String identity, String region, String affiliation, String others) {
        return null;
    }

    @Override
    public PageVO<Users> getUsers(Integer pageNum, Integer pageSize, String searchText,
                                  String startDate, String endDate, String sortType, String status,
                                  String identity, String region, String affiliation, String ethnicity, String gender, String auditStatus) {
        PageVO<Users> pb = new PageVO<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Users> users = adminsMapper.getUsers(searchText, startDate, endDate, sortType,
                status, identity, region, affiliation, ethnicity, gender, auditStatus);
        Page<Users> pag = (Page<Users>) users;
        pb.setTotal(pag.getTotal());
        pb.setItems(pag.getResult());
        return pb;
    }

    @Override
    public List<Users> getUsersForExport(String searchText, String startDate, String endDate, String sortType,
                                         String status, String identity, String region, String affiliation, String ethnicity, String gender, String auditStatus) {
        return adminsMapper.getUsers(searchText, startDate, endDate, sortType, status, identity, region, affiliation, ethnicity, gender, auditStatus);
    }

    @Override
    public boolean resetPassword(Integer uid, String newPassword) {
        return adminsMapper.resetPassword(uid, newPassword) > 0;
    }

    @Override
    public Users getUsersDetails(Integer uid) {
        return adminsMapper.getUsersDetails(uid);
    }

    @Override
    public void updateStatus(Integer uid, String status) {
        adminsMapper.updateStatus(uid, status);
    }

    @Override
    public boolean updateUser(Users user) {
        // 设置更新时间
        user.setUpdateTime(LocalDateTime.now());
        return adminsMapper.updateUser(user) > 0;
    }
    
    @Override
    public boolean deleteUser(Integer uid) {
        return adminsMapper.deleteUser(uid) > 0;
    }


    @Override
    public Map<String, Object> getUserStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        
        // 获取当前用户总数
        Integer totalUsers = adminsMapper.getTotalUsers();
        
        // 获取昨日新增用户数
        Integer newUsersYesterday = adminsMapper.getNewUsersYesterday();
        
        statistics.put("totalUsers", totalUsers);
        statistics.put("newUsersYesterday", newUsersYesterday);
        
        return statistics;
    }

    @Override
    public int getPendingAuditCount() {
        return adminsMapper.getPendingAuditCount();
    }

    @Override
    public List<Map<String, Object>> getUserAffiliationDistribution() {
        return adminsMapper.getUserAffiliationDistribution();
    }

    @Override
    public List<Map<String, Object>> getUserRegistrationTrend(String startDate, String endDate) {
        return adminsMapper.getUserRegistrationTrend(startDate, endDate);
    }
    @Override
    public String getDepartmentName(String departmentId) {
        return  adminsMapper.getDepartmentName(departmentId);
    }

    @Override
    public String getDepartmentName2(String departmentId) {
        return  adminsMapper.getDepartmentName2(departmentId);
    }

    @Override
    public boolean updateUserScore(Integer uid, Double score) {
        return adminsMapper.updateUserScore(uid, score) > 0;
    }

    @Override
    public boolean clearAllScores() {
        return adminsMapper.clearAllScores() > 0;
    }
}
